import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import iview from 'iview'
import 'iview/dist/styles/iview.css'
import qs from 'qs'

Vue.use(iview);
Vue.config.productionTip = false;
Vue.prototype.$http = axios;
Vue.prototype.$uploadUrl = 'http://47.105.45.14:8080/mechanic/service/fileUpload/uploadImage';
  // Vue.prototype.$uploadUrl = 'http://192.168.2.222:8081/mechanic/service/fileUpload/uploadImage';
     //Vue.prototype.$uploadUrl = 'http://192.168.2.103:8081/mechanic/service/fileUpload/uploadImage';
/**
 * axios全局配置
 */
axios.defaults.timeout = 100000;
axios.defaults.baseURL = 'http://47.105.45.14:8080/mechanic/service'
//axios.defaults.baseURL = 'http://192.168.2.222:8081/mechanic/service'
//axios.defaults.baseURL = 'http://192.168.2.103:8081/mechanic/service';
// axios.defaults.baseURL = 'http://192.168.1.105:8081/service';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
/**
 * axios全局配置请求拦截
 */
axios.interceptors.request.use((res) => {
    let authority = JSON.parse(sessionStorage.getItem('Authorization'));
    if(authority){
        res.headers['Authorization'] = authority;
    }
      res.data = qs.stringify(res.data)
      return res
});

/**
 * axios全局配置响应拦截
 */
axios.interceptors.response.use((req) => {
    if(req.status == 200){
        switch (req.data.code) {
            case '401':
                vm.$router.push('/');
                break;
            default:
                return req.data
        }
    }
    return req.data
}, err => {
    console.dir(err)
    console.log(err.response.status)
    const errorCode = err.response.status
    if (errorCode == 401) {
        vm.$router.push('/')
    } else {
        vm.$Notice.destroy()
        vm.$Notice.error({
            title: '系统错误',
            desc: '系统错误，请联系管理员',
            duration: 5,
            name: 'webstatus'
        })
    }
    /*vm.$router.push('/');*/
});

router.beforeEach((to, from, next) => {
    iview.LoadingBar.start();
    next();
});

router.afterEach(route => {
    iview.LoadingBar.finish();
});

Vue.filter('timeForm', function (value) {
  if (typeof (value) == "undefined" || value === null) return "";
 
  let date = new Date(value);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  var d = date.getDate();
  if (isNaN(y) && y != 0) {
    y = " ";
  }
  if (isNaN(m) && m != 0) {
    m = " ";
  } else {
    (m < 10 ? "0" + m : m);
  }
  if (isNaN(d) && d != 0) {
    d = " ";
  } else {
    (d < 10 ? "0" + d : d);
  }
  return y + "-" + m + "-" + d + " " + date.toTimeString().substr(0, 5);
});

var timeForm = Vue.filter('timeForm');
const vm = new Vue({
    router,
    render: h => h(App)
}).$mount('#app');


